package cn.marinda.entity;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.runnable.DownloadServerCoreRunnable;
import cn.marinda.util.HttpUtils;
import cn.marinda.util.ThreadUtils;
import lombok.Data;

import javax.swing.*;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * 服务器核心
 *
 * @author marinda
 * @date 2022/07/14
 */
@Data
public abstract class AbstractServerCore extends AbstractServerResource{

    /**
     * 核心文件名称
     */
    protected String fileName;
//    接入Core下载地址
    protected final String CORE_PATH = "mcCore/";
    protected Server serverDir = Server.SERVER_DOWNLOAD_CORE;
    public static String[] coreList = new String[]{
            "1.6.2","1.7.2","1.8","1.8.8","1.9","1.10","1.11","1.11.2","1.12.2","1.14","1.14.2","1.14.3","1.14.4","1.15","1.15.1","1.15.2","1.16.1","1.16.2","1.16.5","1.17","1.17.1","1.18","1.18.2","1.19","1.19.1"
    };

    /**
     * 无参构造方法
     */
    public  AbstractServerCore(){

    }


    /**
     * 抽象核心服务器
     *
     * @param fileName 文件名称
     */
    public AbstractServerCore(String fileName){
        super(fileName);
        this.fileName = fileName;
    }


    /**
     * 获取核心列表
     *
     * @return {@link String[]}
     */
    @Override
    protected String[] resourceList() {
        return this.coreList;
    }

    /**
     * 服务器 目录
     *
     * @return {@link Server}
     */
    @Override
    protected Server serverDir() {
        return this.serverDir;
    }

    @Override
    public void download(JProgressBar progressBar) {
        //        等于下载地址+核心名.jar
        HttpUtils.serverEnum = this.serverDir;
        if(HttpUtils.isOnlineCore){
            HttpUtils.fileName = HttpUtils.onlineCoreName;
        }else{
            HttpUtils.fileName = "spigot-".concat(this.fileName).concat(".jar");
        }
        HttpUtils.urlPath = URL_PATH.concat(CORE_PATH).concat(HttpUtils.fileName);
        DownloadServerCoreRunnable downloadServerCoreRunnable = new DownloadServerCoreRunnable(progressBar);
        ThreadUtils.downloadScheduled = MinecraftServerHelper.executorService.submit(downloadServerCoreRunnable);
    }

}
